package com.ray.controller.user;

import com.ray.pojo.User;
import com.ray.service.UserService;
import com.ray.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ViewUserController extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        // 判断参数是否存在
        if (req.getParameter("userid") != null)
        {
            // 将参数解析成整形
            int id = Integer.parseInt(req.getParameter("userid"));

            // 实例化UserService对象
            UserService userService = new UserServiceImpl();

            // 获得User实体
            User user = userService.getUserById(id);

            // 将user实体存储到属性中
            req.setAttribute("user", user);

            // 跳转到修改user的页面
            req.getRequestDispatcher("/updateUser.jsp").forward(req, resp);
        }
        else {
            req.getRequestDispatcher("/updateUser.jsp").forward(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        // 如果是post方法，则调用和get方法一样的处理过程
        doGet(req, resp);
    }
}
